function [ S ] = makeMove( S, orient, slot)

S.turn = S.turn + 1;
height = S.top(slot + 1)-S.pBottom{S.nextPiece + 1, orient + 1}(1);

for c = 1:(S.pWidth{S.nextPiece+1}(orient+1)-1)
    height = max(height,S.top(slot+c+1)-S.pBottom{S.nextPiece+1, orient+1}(c+1));
end
	
if (height + S.pHeight{S.nextPiece+1}(orient+1) >= S.rows)
    S.lost = true;
    return;
end

for i = 0:(S.pWidth{S.nextPiece+1}(orient+1)-1)
    for h = (height+S.pBottom{S.nextPiece+1,orient+1}(i+1)):(height+S.pTop{S.nextPiece+1,orient+1}(i+1)-1)
        S.field(h+1,i+slot+1) = S.nextPiece + 1;
    end
end

for c = 0:(S.pWidth{S.nextPiece+1}(orient+1)-1)
    S.top(slot+c+1) = height + S.pTop{S.nextPiece+1, orient+1}(c+1);
end
            
rowsCleared = 0;
for r = (height+S.pHeight{S.nextPiece+1}(orient+1)-1):-1:height
    full = true;
    for c = 0:(S.cols-1) 
        if S.field(r+1,c+1) == 0
            full = false;
            break;
        end
    end

    if full
        rowsCleared = rowsCleared + 1;
        S.cleared = S.cleared +1 ;
        for c = 0:(S.cols-1)
            for i = r:(S.top(c+1)-1)
                S.field(i+1,c+1) = S.field(i+2,c+1);
            end
            S.top(c+1) = S.top(c+1) - 1;
            while (S.top(c+1) >=1 && S.field(S.top(c+1),c+1) ==0)	
                S.top(c+1) = S.top(c+1) - 1; 
            end
        end
    end
    
    S.nextPiece = floor(rand()*S.n_pieces);
end